package com.puzzlebazar.client.squaregrid.view;

import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.HasAllKeyHandlers;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.TextBox;
import com.puzzlebazar.client.util.Vec2i;

public class TextBoxDrawable extends Drawable implements
	HasText, HasChangeHandlers, HasAllKeyHandlers {

	private final TextBox textBox = new TextBox();
	
	/**
	 * Creates a text-box drawable
	 * 
	 * @param name Name of the drawable for future reference (can be null)
	 * @param initialText Text to initially place in the textbox
	 */
	public TextBoxDrawable(String name, String initialText ) {
		super(name);
		setText( initialText );
		setWidget( textBox );
	}

	/**
	 * Creates a text-box drawable
	 * 
	 * @param loc Location of the cell
	 * @param name Name of the drawable for future reference (can be null)
	 * @param initialText Text to initially place in the textbox
	 */
	public TextBoxDrawable(Vec2i loc, String name, String initialText ) {
		super(loc, name);
		setText( initialText );
		setWidget( textBox );
	}

	@Override
	public void setText( String text ) {
		textBox.setText( text );
	}
	
	@Override
	public String getText() {
		return textBox.getText();
	}

	@Override
	public HandlerRegistration addChangeHandler(ChangeHandler handler) {
		return textBox.addChangeHandler(handler);
	}

	@Override
	public HandlerRegistration addKeyUpHandler(KeyUpHandler handler) {
		return textBox.addKeyUpHandler(handler);
	}

	@Override
	public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
		return textBox.addKeyDownHandler(handler);
	}

	@Override
	public HandlerRegistration addKeyPressHandler(KeyPressHandler handler) {
		return textBox.addKeyPressHandler(handler);
	}
	
	@Override
	public void fireEvent(GwtEvent<?> event) {
		textBox.fireEvent(event);		
	}

	
	/**
	 * Explicitly focus/unfocus this widget. Only one widget can have focus at a
	 * time, and the widget that does will receive all keyboard events.
	 * 
	 * @param focused whether this widget should take focus or release it
	 */
	public void setFocus(boolean focused) {
		textBox.setFocus(focused);
	}

	/**
	 * Selects all of the text in the box.
	 * 
	 * This will only work when the widget is attached to the document.
	 */
	public void selectAll() {
		textBox.selectAll();
	}

	/**
	 * Sets the maximum allowable length of the text box.
	 * 
	 * @param length the maximum length, in characters
	 */
	public void setMaxLength(int length) {
		textBox.setMaxLength(length);
	}

}
